function socketOnMessage(_event){
try
{
var baProcessData = null;
var intRawPktLen;
var intCompLen = 0;
var _response, isBroken = false;
var totalPacketLength = 0;
if (_event.data instanceof ArrayBuffer) {
var dataReceived = null;
var dataPacketLengthList = [];
if (baOldData == null)
dataReceived = new Uint8Array(_event.data);
else {
dataReceived = AppendOrCopyBuffer(baOldData, _event.data);
baOldData = null;
}
intRawPktLen = dataReceived.byteLength;
var i = 0;
if (intRawPktLen > 5) {
while (i < intRawPktLen) {
if (dataReceived[i] == 5 || dataReceived[i] == 2) {
var strPacketLength = String.fromCharCode.apply(null, dataReceived.subarray(i + 1, i + 6));
if (strPacketLength.length == 5) {
var packetLength = parseInt(strPacketLength, 10);
dataPacketLengthList.push(packetLength + 6);
totalPacketLength += packetLength + 6;
i = i + 6 + packetLength;
}
else {
baOldData = dataReceived.subarray(i, intRawPktLen);
isBroken = true;
break;
}
}
}
}
else
baOldData = dataReceived;
if (intRawPktLen == totalPacketLength) {
// split and pass to zlib to uncompress
for (var i = 0, j = 0, k = dataPacketLengthList[0], len = dataPacketLengthList.length; i < len ; i++) {
var uncompData = dataReceived.subarray(j, k);
ProcessSocketMessage(uncompData);
j = k;
k = k + dataPacketLengthList[i + 1];
}
baOldData = null;
}
else {
var i = 0, j = 0;
var k = (dataPacketLengthList.length > 0 ? dataPacketLengthList[0] : 0);
if (!isBroken) {
for (var len = dataPacketLengthList.length ; i < len - 1; i++) {
var uncompData = dataReceived.subarray(j, k);
ProcessSocketMessage(uncompData);
j = k;
k = k + dataPacketLengthList[i + 1];
}
if (i == dataPacketLengthList.length - 1) {
baOldData = null;
baOldData = dataReceived.subarray(j, k);
if (baOldData[0] != 5 && baOldData[0] != 2)
alert();
}
}
else {
for (var len = dataPacketLengthList.length; i < len; i++) {
var uncompData = dataReceived.subarray(j, k);
ProcessSocketMessage(uncompData);
j = k;
k = k + dataPacketLengthList[i + 1];
}
isBroken = false;
}
}
}
}
catch(ex)
{
}
}
function ProcessSocketMessage(uncompData) {
_response = DeCompressData(uncompData);
if (_response == undefined) {
console.log(_response);
}
//remove End of response char
var intTmtrIndex = _response.indexOf('\u0000');
if (intTmtrIndex != -1) {
_response = _response.substr(0, intTmtrIndex);
}
//split multi response packet with Start of response char C_S_CHAR2
var arrData = _response.split("\u0002");
var intDataCount = arrData.length;
for (var intDataCntr = 0; intDataCntr < intDataCount; intDataCntr++) {
if (arrData[intDataCntr] != "") {
console.log("Packet: " + arrData[intDataCntr]);
}
}
}